package
{
	import flash.display.Sprite;
	import game.UI.ProgressBar;
	import starling.core.Starling;
	
	/**
	 * @author Kirill Pletnev
	 */
	public class SplashScreen extends SplashScreenUI
	{
		private static const MAX_VALUE:Number = 1.0;
		private static var _instance:SplashScreen;
		
		private var _numSteps:int;
		private var _stepDiv:Number;
		
		private var _loadingBar:ProgressBar;
		
		public function SplashScreen()
		{
			if (_instance != null)
			{
				throw new Error("SplashScreen instance already existing");
			}
		}
		
		static public function get instance():SplashScreen
		{
			if (_instance == null)
			{
				_instance = new SplashScreen();
			}
			return _instance;
		}
		
		private function init():void
		{
			mouseEnabled = false;
			mouseChildren = false;
			
			var sprite:Sprite = progressBar.barMc;
			_loadingBar = new ProgressBar(sprite);
		}
		
		public function nextStep():void
		{
			_loadingBar.currentValue += _stepDiv;
		}
		
		public function show():void
		{
			init();
			
			_loadingBar.setValues(0, MAX_VALUE, true);
			Starling.current.nativeOverlay.addChild(this);
		}
		
		public function hide():void
		{
			Starling.current.nativeOverlay.removeChild(this);
			
			_instance = null;
			_loadingBar = null;
		}
		
		public function get numSteps():int 
		{
			return _numSteps;
		}
		
		public function set numSteps(value:int):void 
		{
			_stepDiv = MAX_VALUE / value;
			_numSteps = value;
		}
	}
}